<template>
  <div class="app-container">
    <ResizeBox :treeData="treeData" @pitchOnData="pitchOnData">
      <template>
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="80px"
        >
          <el-form-item label="单元名称" prop="unitName">
            <el-input
              v-model="queryParams.unitName"
              placeholder="请输入单元名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-table
          v-loading="loading"
          :data="evaluationList"
          highlight-current-row
          @current-change="handleCurrentChange"
        >
          <el-table-column
            label="安全风险对象"
            align="center"
            prop="objectName"
          />
          <el-table-column
            label="安全风险单元"
            align="center"
            prop="unitName"
          />
          <el-table-column label="判定方法" align="center" prop="method" />
          <el-table-column
            label="风险等级"
            align="center"
            prop="maxAccidentLevel"
          />
          <el-table-column
            label="风险识别成员"
            align="center"
            prop="evaluationUsers"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                round
                size="small"
                type="primary"
                icon="el-icon-finished"
                @click="handleAdd(scope.row)"
                v-hasPermi="['syf:evaluation:add']"
                v-if="scope.row.id == null"
                >判定</el-button
              >
              <el-button
                round
                size="small"
                type="info"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['syf:evaluation:edit']"
                v-if="scope.row.id != null"
                >修改</el-button
              >
              <el-button
                round
                size="small"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['syf:evaluation:remove']"
                v-if="scope.row.id != null"
                >取消</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改风险判定主对话框 -->
        <el-dialog
          :title="title"
          :visible.sync="open"
          width="900px"
          append-to-body
        >
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="安全风险对象" prop="objectName">
                  <el-input
                    v-model="form.objectName"
                    placeholder="安全风险对象"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="安全风险单元" prop="unitName">
                  <el-input
                    v-model="form.unitName"
                    placeholder="安全风险单元"
                    disabled
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="24">
                <el-form-item prop="accidentList" label="风险事件:">
                  <el-select
                    v-model="form.accidentList"
                    multiple
                    placeholder="请选择风险事件"
                    style="width: 100%"
                    @change="accidentChange"
                  >
                    <el-option
                      v-for="item in accidentData"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="判定方法" prop="method">
                  <el-select
                    v-model="form.method"
                    placeholder="请选择判定方法"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in dict.type.decision_method"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="风险等级" prop="maxAccidentLevel">
                  <el-select
                    v-model="form.maxAccidentLevel"
                    placeholder="所选风险事件最高风险等级"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in dict.type.risk_level"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="24">
                <el-form-item label="风险识别成员" prop="evaluationUsers">
                  <el-input
                    v-model="form.evaluationUsers"
                    type="textarea"
                    placeholder="请输入内容"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </template>
    </ResizeBox>
  </div>
</template>

<script>
import {
  listEvaluation,
  getEvaluation,
  getEvaluationByUnitId,
  delEvaluation,
  addEvaluation,
  updateEvaluation,
} from "@/api/syf/risk/evaluation";
import { listType } from "@/api/syf/risk/type";
import { listObject } from "@/api/syf/risk/object";
import ResizeBox from "@/components/syf/ResizeBox/index";

export default {
  name: "Evaluation",
  components: { ResizeBox },
  dicts: ["risk_level", "decision_method"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 风险判定主表格数据
      evaluationList: [],
      // 风险数据集
      accidentData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 树选项
      treeData: undefined,
      // 列表选中的数据
      currentRow: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        unitName: null,
        objectIds: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        method: [
          { required: true, message: "判定方法不能为空", trigger: "blur" },
        ],
        accidentList: [
          { required: true, message: "风险事件不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getTreeData();
    this.getAccidentData();
    this.getList();
  },
  methods: {
    /** 查询风险判定主列表 */
    getList() {
      this.loading = true;
      listEvaluation(this.queryParams).then((response) => {
        this.evaluationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        objectId: null,
        unitId: null,
        method: null,
        maxAccidentLevel: null,
        status: null,
        evaluationUsers: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        firmId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 单选选中数据
    handleCurrentChange(val) {
      this.currentRow = val;
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      const unitId = row.unitId;
      getEvaluationByUnitId(unitId).then((response) => {
        this.form = response.data;
        this.form.objectId = row.objectId;
        this.form.objectName = row.objectName;
        this.form.unitId = row.unitId;
        this.form.unitName = row.unitName;
        this.open = true;
        this.title = "风险判定";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      if (id == null) {
        this.$message({
          message: "未进行风险判定，请先判定!",
          type: "warning",
        });
        return;
      }
      getEvaluation(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改风险判定";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateEvaluation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEvaluation(this.form).then((response) => {
              this.$modal.msgSuccess("判定成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      if (id == null) {
        return;
      }
      this.$modal
        .confirm('是否确认删除风险判定主编号为"' + id + '"的数据项？')
        .then(function () {
          return delEvaluation(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 查询风险事件数据集 */
    getAccidentData() {
      listType().then((response) => {
        this.accidentData = response.rows;
      });
    },
    /** 查询树数据 */
    getTreeData() {
      listObject().then((res) => {
        let tree = [];
        res.rows.forEach((element) => {
          tree.push({
            id: element.id,
            name: element.name,
          });
        });
        this.treeData = tree;
      });
    },
    //选中树节点
    pitchOnData(val) {
      let objectIds = [];
      val.forEach((item) => {
        objectIds.push(item.id);
      });
      this.queryParams.objectIds = objectIds;
      this.handleQuery();
    },
    //选中风险事件
    accidentChange() {
      let level = [];
      this.form.accidentList.forEach((a) => {
        this.accidentData.forEach((b) => {
          if (a == b.id) {
            level.push(b.level);
            return;
          }
        });
      });
      level.sort();
      this.form.maxAccidentLevel = level[0];
    },
  },
};
</script>
